home *** CD-ROM | disk | FTP | other *** search
/ 100 Best Shareware & Freeware Games / 100 Games.iso / Cards / PySol / pysol460.exe / {app} / python / Lib / exceptions.py < prev    next >
Encoding:
Python Source  |  2001-07-27  |  6.8 KB  |  241 lines

  1. """Class based built-in exception hierarchy.
  2.  
  3. New with Python 1.5, all standard built-in exceptions are now class objects by
  4. default.  This gives Python's exception handling mechanism a more
  5. object-oriented feel.  Traditionally they were string objects.  Python will
  6. fallback to string based exceptions if the interpreter is invoked with the -X
  7. option, or if some failure occurs during class exception initialization (in
  8. this case a warning will be printed).
  9.  
  10. Most existing code should continue to work with class based exceptions.  Some
  11. tricky uses of IOError may break, but the most common uses should work.
  12.  
  13. Here is a rundown of the class hierarchy.  You can change this by editing this
  14. file, but it isn't recommended because the old string based exceptions won't
  15. be kept in sync.  The class names described here are expected to be found by
  16. the bltinmodule.c file.  If you add classes here, you must modify
  17. bltinmodule.c or the exceptions won't be available in the __builtin__ module,
  18. nor will they be accessible from C.
  19.  
  20. The classes with a `*' are new since Python 1.5.  They are defined as tuples
  21. containing the derived exceptions when string-based exceptions are used.  If
  22. you define your own class based exceptions, they should be derived from
  23. Exception.
  24.  
  25. Exception(*)
  26.  |
  27.  +-- SystemExit
  28.  +-- StandardError(*)
  29.       |
  30.       +-- KeyboardInterrupt
  31.       +-- ImportError
  32.       +-- EnvironmentError(*)
  33.       |    |
  34.       |    +-- IOError
  35.       |    +-- OSError(*)
  36.       |         |
  37.       |         +-- WindowsError(*)
  38.       |
  39.       +-- EOFError
  40.       +-- RuntimeError
  41.       |    |
  42.       |    +-- NotImplementedError(*)
  43.       |
  44.       +-- NameError
  45.       |    |
  46.       |    +-- UnboundLocalError(*)
  47.       |
  48.       +-- AttributeError
  49.       +-- SyntaxError
  50.       +-- TypeError
  51.       +-- AssertionError
  52.       +-- LookupError(*)
  53.       |    |
  54.       |    +-- IndexError
  55.       |    +-- KeyError
  56.       |
  57.       +-- ArithmeticError(*)
  58.       |    |
  59.       |    +-- OverflowError
  60.       |    +-- ZeroDivisionError
  61.       |    +-- FloatingPointError
  62.       |
  63.       +-- ValueError
  64.       +-- SystemError
  65.       +-- MemoryError
  66. """
  67.  
  68. class Exception:
  69.     """Proposed base class for all exceptions."""
  70.     def __init__(self, *args):
  71.         self.args = args
  72.  
  73.     def __str__(self):
  74.         if not self.args:
  75.             return ''
  76.         elif len(self.args) == 1:
  77.             return str(self.args[0])
  78.         else:
  79.             return str(self.args)
  80.  
  81.     def __getitem__(self, i):
  82.         return self.args[i]
  83.  
  84. class StandardError(Exception):
  85.     """Base class for all standard Python exceptions."""
  86.     pass
  87.  
  88. class SyntaxError(StandardError):
  89.     """Invalid syntax."""
  90.     filename = lineno = offset = text = None
  91.     msg = ""
  92.     def __init__(self, *args):
  93.         self.args = args
  94.         if len(self.args) >= 1:
  95.             self.msg = self.args[0]
  96.         if len(self.args) == 2:
  97.             info = self.args[1]
  98.             try:
  99.                 self.filename, self.lineno, self.offset, self.text = info
  100.             except:
  101.                 pass
  102.     def __str__(self):
  103.         return str(self.msg)
  104.  
  105. class EnvironmentError(StandardError):
  106.     """Base class for I/O related errors."""
  107.     def __init__(self, *args):
  108.         self.args = args
  109.         self.errno = None
  110.         self.strerror = None
  111.         self.filename = None
  112.         if len(args) == 3:
  113.             # open() errors give third argument which is the filename.  BUT,
  114.             # so common in-place unpacking doesn't break, e.g.:
  115.             #
  116.             # except IOError, (errno, strerror):
  117.             #
  118.             # we hack args so that it only contains two items.  This also
  119.             # means we need our own __str__() which prints out the filename
  120.             # when it was supplied.
  121.             self.errno, self.strerror, self.filename = args
  122.             self.args = args[0:2]
  123.         if len(args) == 2:
  124.             # common case: PyErr_SetFromErrno()
  125.             self.errno, self.strerror = args
  126.  
  127.     def __str__(self):
  128.         if self.filename is not None:
  129.             return '[Errno %s] %s: %s' % (self.errno, self.strerror,
  130.                                           repr(self.filename))
  131.         elif self.errno and self.strerror:
  132.             return '[Errno %s] %s' % (self.errno, self.strerror)
  133.         else:
  134.             return StandardError.__str__(self)
  135.  
  136. class IOError(EnvironmentError):
  137.     """I/O operation failed."""
  138.     pass
  139.  
  140. class OSError(EnvironmentError):
  141.     """OS system call failed."""
  142.     pass
  143.  
  144. class WindowsError(OSError):
  145.     """MS-Windows OS system call failed."""
  146.     pass
  147.  
  148. class RuntimeError(StandardError):
  149.     """Unspecified run-time error."""
  150.     pass
  151.  
  152. class NotImplementedError(RuntimeError):
  153.     """Method or function hasn't been implemented yet."""
  154.     pass
  155.  
  156. class SystemError(StandardError):
  157.     """Internal error in the Python interpreter.
  158.  
  159.     Please report this to the Python maintainer, along with the traceback,
  160.     the Python version, and the hardware/OS platform and version."""
  161.     pass
  162.  
  163. class EOFError(StandardError):
  164.     """Read beyond end of file."""
  165.     pass
  166.  
  167. class ImportError(StandardError):
  168.     """Import can't find module, or can't find name in module."""
  169.     pass
  170.  
  171. class TypeError(StandardError):
  172.     """Inappropriate argument type."""
  173.     pass
  174.  
  175. class ValueError(StandardError):
  176.     """Inappropriate argument value (of correct type)."""
  177.     pass
  178.  
  179. class KeyboardInterrupt(StandardError):
  180.     """Program interrupted by user."""
  181.     pass
  182.  
  183. class AssertionError(StandardError):
  184.     """Assertion failed."""
  185.     pass
  186.  
  187. class ArithmeticError(StandardError):
  188.     """Base class for arithmetic errors."""
  189.     pass
  190.  
  191. class OverflowError(ArithmeticError):
  192.     """Result too large to be represented."""
  193.     pass
  194.  
  195. class FloatingPointError(ArithmeticError):
  196.     """Floating point operation failed."""
  197.     pass
  198.  
  199. class ZeroDivisionError(ArithmeticError):
  200.     """Second argument to a division or modulo operation was zero."""
  201.     pass
  202.  
  203. class LookupError(StandardError):
  204.     """Base class for lookup errors."""
  205.     pass
  206.  
  207. class IndexError(LookupError):
  208.     """Sequence index out of range."""
  209.     pass
  210.  
  211. class KeyError(LookupError):
  212.     """Mapping key not found."""
  213.     pass
  214.  
  215. class AttributeError(StandardError):
  216.     """Attribute not found."""
  217.     pass
  218.  
  219. class NameError(StandardError):
  220.     """Name not found globally."""
  221.     pass
  222.  
  223. class UnboundLocalError(NameError):
  224.     """Local name referenced but not bound to a value."""
  225.     pass
  226.  
  227. class MemoryError(StandardError):
  228.     """Out of memory."""
  229.     pass
  230.  
  231. class SystemExit(Exception):
  232.     """Request to exit from the interpreter."""
  233.     def __init__(self, *args):
  234.         self.args = args
  235.         if len(args) == 0:
  236.             self.code = None
  237.         elif len(args) == 1:
  238.             self.code = args[0]
  239.         else:
  240.             self.code = args
  241.